//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Drawing;
using System.Collections;
using System.Collections.Specialized;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommunityServer;
using CommunityServer.Components;
using System.ComponentModel;
using System.IO;

namespace CommunityServer.Controls 
{
    // *********************************************************************
    //  WhoIsOnline
    //
    /// <summary>
    /// This server control is used to display a list of users currently online.
    /// </summary>
    // ***********************************************************************/
    public class WhoIsOnline : TemplatedWebControl
    {
        #region Child Controls

        Literal userCount;
        Repeater usersOnline;
        Repeater moderatorsOnline;
        Literal guestUsers;
        System.Web.UI.WebControls.Image img;

        CSContext csContext = CSContext.Current;

        #endregion

        protected override void OnInit(EventArgs e) 
        {
            if (SkinName == null)                
                ExternalSkinFileName = "Skin-WhoIsOnline.ascx";
            else 
                ExternalSkinFileName = SkinName;
			
            base.OnInit(e);
        }
		
        protected override void OnLoad(EventArgs e) 
        {
            // If the user is already authenticated we have no work to do
            //
            if (!csContext.SiteSettings.EnableWhoIsOnline)
            {
                this.Visible = false;
                return;
            }

            if (!Page.IsPostBack)
                DataBind();

            base.OnLoad(e);
        }

        #region DataBind

        public override void DataBind() 
        {
            base.DataBind();
            
            // Get statistics
            //
            SiteStatistics siteStats = csContext.Statistics;
            IList users = UsersOnline.Members();

            // Stats image
            //
            if (null != img)
                img.ImageUrl = Globals.GetSkinPath() + "/images/user_IsOnline.gif";

            // Display total users
            //
            if (null != userCount) 
            {
                string members = siteStats.TotalUsers.ToString("n0");

                if (csContext.SiteSettings.EnablePublicMemberList) 
                {
                    members = "<a href=\"" + Globals.GetSiteUrls().UserList + "\">" + members + "</a>";
                }
                
                if (users != null && users.Count > 0)
                    userCount.Text = String.Format(CommunityServer.Components.ResourceManager.GetString("WhoIsOnline_UsersOnlineCount"), users.Count.ToString("n0"), members); 
                else
                    userCount.Text = CommunityServer.Components.ResourceManager.GetString("WhoIsOnline_NoUsersOnline"); 
            }

            // Display users online
            //
            if (usersOnline != null && users != null && users.Count > 0) 
            {
                usersOnline.DataSource = users;
                usersOnline.DataBind();
            }

            // Display moderators online
            //
            if (moderatorsOnline != null && users != null && users.Count > 0) 
            {
                IList moderators = Users.GetLoggedInModerators();
                ArrayList filter = new ArrayList(moderators.Count);
                foreach(User user in moderators)
                {
                    foreach(UserOnline ou in users)
                    {
                        if(ou.UserID == user.UserID)
                            filter.Add(ou);
                    }
                }

                moderatorsOnline.DataSource = filter;
                moderatorsOnline.DataBind();
            }

            // Guests online
            //
            if (null != guestUsers)
            {
                int guestsCount = UsersOnline.GuestCount;

//                if (siteStats.CurrentAnonymousUsers > 1)
//                    guestsCount = siteStats.CurrentAnonymousUsers;

                guestUsers.Text = String.Format(CommunityServer.Components.ResourceManager.GetString("WhoIsOnline_GuestUsers"), guestsCount.ToString("n0"));
            }
        }
		
        #endregion		
		
        #region Skin
                
        protected override void AttachChildControls() 
        {
            img = (System.Web.UI.WebControls.Image) FindControl("StatsImg");
            userCount = (Literal) FindControl("UsersOnlineCount");
            usersOnline = (Repeater) FindControl("UsersOnline");
            moderatorsOnline = (Repeater) FindControl("ModeratorsOnline");
            guestUsers = (Literal) FindControl("GuestUsers");
        }
		        
        #endregion
    }
}
